package com.car.account.util;

import com.car.account.dto.geo.AddressResult;
import com.car.account.dto.geo.AddressResult2;
import com.car.commons.core.model.WoniuException;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class GEOUtil {
    private RestTemplate restTemplate=new RestTemplate();


    /**
     * 将地址转换为经纬度
     */
    private static final String addressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?address={address}&key={key}";
//    private static final String addressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?address={address}&key={key}";
    private static final String key = "Z76BZ-OXEL7-WBDXC-PL4L7-CCBAE-4LF2Q";

    public AddressResult2 addressConvert(String address) throws Exception{
        AddressResult result = restTemplate.getForObject(addressUrl, AddressResult.class, address, key);
        if (result.getStatus() != 0) {
            throw new WoniuException(500, result.getMessage());
        }
        if (!(result.getResult().getLevel() >= 9 && result.getResult().getReliability() >= 7)) {
            throw new WoniuException(500, "地址不准确");
        }
        return result.getResult().getLocation();
    }


}
